package xujiwen.transport.Mgr;

import java.util.ArrayList;
import java.util.List;

import xujiwen.transport.bean.EnterRecord;
import xujiwen.transport.util.DBConnect;

public class EnterRecordMgr {
	public void Save(final EnterRecord enterRecord) {
		final DBConnect db = new DBConnect();
		final String sql = "insert into ENTRYRECORD values (null, ?, ?, ?, '行驶在车道中..', now())";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setString(1, enterRecord.getIcid());
			db.setString(2, enterRecord.getCid());
			//db.setInt(3, enterRecord.getCarClass());
			db.setString(3, enterRecord.getEntryName());
			db.excute();
			db.commit();

		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
	// 按车牌号 入口站查找记录
	public List<EnterRecord> GetSearchERList(String str) {
		final List<EnterRecord> er = new ArrayList<EnterRecord>();
		final DBConnect db = new DBConnect();
		final String sql;
		if(str.length() > 0)
		sql = "select CARINFO.CARTYPE,CARINFO.CARCLASS,ENTRYRECORD.* " + 
		"from CARINFO,ENTRYRECORD where ENTRYRECORD.CID=CARINFO.CID " + 
		"ENTRYRECORD.EntryName like '%" + str +"%' or ENTRYRECORD.CID like '%" + str +"%' "+
		"order by ENTERDATE desc";
		else sql = "select CARINFO.CARTYPE,CARINFO.CARCLASS,ENTRYRECORD.* " + 
		"from CARINFO,ENTRYRECORD where ENTRYRECORD.CID=CARINFO.CID " + 
		"order by ENTRYRECORD.ENTERDATE desc limit 10";
		//System.out.println(sql);
		db.prepareStatement(sql);
		db.excuteQuery();
		try {
			while (db.next()) {
				final EnterRecord enterRecord = new EnterRecord();
				enterRecord.setEid(db.getInt("EID"));
				enterRecord.setIcid(db.getString("ICID"));
				enterRecord.setCid(db.getString("CID"));
				enterRecord.setCarType(db.getString("CARTYPE"));
				enterRecord.setCarClass(db.getInt("CARCLASS"));
				enterRecord.setEntryName(db.getString("EntryName"));
				enterRecord.setEnterDate(db.getTimestamp("ENTERDATE"));
				enterRecord.setState(db.getString("STATE"));
				er.add(enterRecord);
			}
		} catch (final Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			db.close();
		}
		return er;
	}
	
	// 通过IC卡 得到进站记录
	public EnterRecord GetRecordByIC(final String ic) {
		EnterRecord enterRecord = null;
		final DBConnect db = new DBConnect();
		final String sql = "select CARINFO.CARTYPE,CARINFO.CARCLASS,ENTRYRECORD.* from" + 
		" CARINFO,ENTRYRECORD where ENTRYRECORD.CID=CARINFO.CID and ICID=? order by ENTERDATE desc limit 1";
		db.prepareStatement(sql);
		db.setString(1, ic);
		db.excuteQuery();
		try {
			while (db.next()) {
				enterRecord = new EnterRecord();
				enterRecord.setEid(db.getInt("EID"));
				enterRecord.setIcid(db.getString("ICID"));
				enterRecord.setCid(db.getString("CID"));
				enterRecord.setCarType(db.getString("CARTYPE"));
				enterRecord.setCarClass(db.getInt("CARCLASS"));
				enterRecord.setEntryName(db.getString("EntryName"));
				enterRecord.setEnterDate(db.getTimestamp("ENTERDATE"));
				enterRecord.setState(db.getString("STATE"));
				//enterRecord.add(enterRecord);
			}
		} catch (final Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			db.close();
		}
		return enterRecord;
	}
	
	public void Del(final int eid)
	{
		final DBConnect db = new DBConnect();
		final String sql = "delete from ENTRYRECORD where EID = ?";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setInt(1, eid);
			db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
	
	/*public String EntryStatistic(final String entryName)
	{
		final DBConnect db = new DBConnect();
		final String sql = "select (count*) from ENTRYRECORD where EID = ?";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setString(1, entryName);
			db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}*/
}
